package com.jt;

import org.omg.CORBA.PRIVATE_MEMBER;

import java.util.Iterator;

public class MyQueue<T>   implements  Iterable<T>{

    private  Node<T>  head;

    private   Node<T>  last;

    private  int size;


    //判断该集合是不是为空
    public  boolean  isEmpty(){
        return size==0;
    }

    //初始化该集合


    public MyQueue() {
        head=null;
        size=0;
    }

    //查看队列的大小
    public  int  size(){
        return  size;
    }

    //添加元素的方法
    public  void  inQ(T t){
        if(last==null){
            head=new Node(t, null);
            last=head;
        }else {
            Node newHead = new Node(t, null);
           last.next=newHead;
           last=newHead;
        }
        size++;
    }

    //获取队列元素的方式
    public  T  outQ(){
        if(head!=null){
            Node<T> curHead = this.head;
            head=head.next;
            return curHead.item;
        }
        size--;
        return null;
    }

    private class Node<T>{
        T  item;

        Node<T> next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }


    class  MyIterator  implements  Iterator<T>{
        private Node<T>  node;

        public MyIterator() {
            this.node = head;
        }

        @Override
        public boolean hasNext() {
            if (node!=null)
                return true;
            return false;
        }

        @Override
        public T next() {
            Node<T> currhead = node;
            node=node.next;
            return currhead.item;
        }
    }



    @Override
    public Iterator<T> iterator() {
        return new MyIterator();
    }
}
